<template>
  <div id="panel">
    <template>
      <el-card class="queryForm">
        <el-form
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          :label-position="'right'"
        >
          <el-form-item label="站点名称：" prop="stationname">
            <el-input
              v-model="queryParams.stationname"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="站点编码：" prop="stationcode">
            <el-input
              v-model="queryParams.stationcode"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="选择区划：" prop="regionCode" class="times">
            <el-select
              size="small"
              v-model="queryParams.regionCode"
              placeholder="请选择行政区划"
              clearable
            >
              <el-option
                v-for="item in regionList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="是否大于零" prop="stationCode">
            <el-switch
              v-model="queryParams.stationCode"
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </el-form-item> -->
          <el-form-item label="时间范围">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>

          <div class="searchBtnBox">
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleQuery"
              style="padding: 10px"
              >查询</el-button
            >
            <el-button @click="resetQuery" style="padding: 10px">
              <i class="iconfont icon-saoba"></i>
              清除
            </el-button>
          </div>
        </el-form>
      </el-card>
      <el-card class="pageContent">
        <el-table :data="tableList.rows" stripe :height="tableHeight">
          <el-table-column type="index" label="序号" />
          <el-table-column prop="stationname" label="站点名称" />
          <el-table-column prop="stationcode" label="站点编码" />
          <el-table-column prop="rainTypeName" label="站点类型" />
          <el-table-column prop="regionName" label="区县" />
          <el-table-column prop="sampletime" label="监测时间" />
          <el-table-column prop="rainValue" label="降雨量(mm)" />
        </el-table>
        <el-pagination
          class="pagination-self"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :pager-count="5"
          :current-page="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          layout="slot, total, sizes, prev, pager, next, jumper"
          :total="tableList.total"
        >
          <el-button
            class="refresh-button"
            icon="el-icon-refresh"
            title="刷新"
            @click="getList"
          />
        </el-pagination>
      </el-card>
    </template>
  </div>
</template>

<script>
import dayjs from "dayjs"; // 引入 day.js
import { getRegionList } from "@/api/dataResources/monitorData/region.js";
import { getRainDataList } from "@/api/dataResources/waterJcData/jcsection";
export default {
  name: "HydrologicStation",
  dicts: ["sys_normal_disable", "sys_user_sex"],
  components: {},
  computed: {},
  data() {
    return {
      tableHeight: window.innerHeight - 400,
      options: [
        { value: "河流1", label: "河流1" },
        { value: "河流2", label: "河流2" },
        { value: "河流3", label: "河流3" },
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationname: "",
        stationcode: "",
        regionCode: "",
      },
      tableList: {
        total: 0,
        rows: [],
      },
      dateRange: [],
      // 选中数组
      ids: [],
      tableData: [
        {
          date: "2016-05-02",
          name: "绿萃厂界",
          province: "2.52",
          lhq: "-",
          an: "-",
          voc: "0.250",
          fx: "东北偏东风",
          sd: "-",
          dqy: "-",
          ou: "-",
          swwd: "19.88",
          city: "普陀区",
          address: "上海市普陀区金沙江路 1518 弄",
          zip: 200333,
        },
      ],
      // 是否显示新增/修改弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "用户名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        deptId: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
      },
      // 行政区划项
      regionList: [],
    };
  },
  watch: {},
  mounted() {
    this.pageInit();
  },
  methods: {
    //页面初始化请求的数据
    pageInit() {
      this.setDefaultWeekDate(); // 设置默认最近一周时间
      this.getRegionList();
      this.getList();
    },
    // 设置默认时间：最近一周（结束时间=今天，开始时间=今天-6天）
    setDefaultWeekDate() {
      const endDate = dayjs().format("YYYY-MM-DD"); // 今天
      const startDate = dayjs().subtract(6, "day").format("YYYY-MM-DD"); // 三天前（今天减去2天）
      this.dateRange = [startDate, endDate];
    },
    // 获取行政区划项
    async getRegionList() {
      let res = await getRegionList();
      if (res.code == 200) {
        this.regionList = res.rows;
      }
    },
    //表格checkbox选择数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal
        .confirm('是否确认删除用户编号为"' + userIds + '"的数据项？')
        .then(function () {
          // return delUser(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 行间编辑
    edit(row) {
      this.open = true;
      // this.formType = "edit";
      this.title = "修改";
      this.form = { ...row };
      this.form.lifeStatus = row.lifeStatus === "1";
    },
    // 删除行
    delRows(row) {
      this.$confirm("是否确认删除该行数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 调用删除接口
          this.$message.success("删除成功");
          this.getList();
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryParams.pageNum = val;
      this.getList();
    },
    //搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //重置、清除表单
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
    },
    // 弹出层取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stationName: "",
        riverName: "",
        longitude: "",
        latitude: "",
        sex: "",
        address: "",
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.userId != undefined) {
            // updateUser(this.form).then(response => {
            //   this.$modal.msgSuccess("修改成功")
            //   this.open = false
            //   this.getList()
            // })
            this.open = false;
            this.$modal.msgSuccess("修改成功");
          } else {
            // addUser(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功")
            //   this.open = false
            //   this.getList()
            // })
            this.open = false;
            this.$modal.msgSuccess("新增成功");
          }
        }
      });
    },
    //导出
    handleExport() {},
    // 获取表格数据
    getList() {
      let query = JSON.parse(JSON.stringify(this.queryParams));
      if (this.dateRange && this.dateRange.length > 0) {
        query.beginTime = this.dateRange[0];
        query.endTime = this.dateRange[1];
      }
      getRainDataList(query).then((res) => {
        console.log(">>>表格数据", res);
        this.tableList.total = res.total;
        this.tableList.rows = res.rows;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
p {
  margin: 0;
}

img {
  vertical-align: middle;
}

#panel {
  padding-bottom: 40px;
  min-height: calc(100% - 20px);
  margin: 0;

  // 控制分页的样式----二次开发
  .pagination-self {
    margin: 20px 0;
    display: flex;
    justify-content: flex-end;
    position: relative;

    .el-pagination__total {
      position: absolute;
      left: 5px;
      color: #409eff;
      font-weight: 600;
      font-size: 15px;
    }
  }

  // 控制分页刷新按钮样式
  .refresh-button {
    .el-icon-refresh {
      font-size: 18px;
      line-height: 28px;
    }
  }

  .el-card {
    margin: 10px;
  }

  .queryForm .el-card__body {
    padding: 0 20px !important;
  }

  .queryForm {
    // .el-card__body {
    //   padding-left: 0 !important;
    // }
    .el-form-item {
      margin: 20px 0 20px 20px;
    }

    .el-form-item:first-child {
      margin-left: 0 !important;
    }

    .el-form-item__label {
      padding-right: 5px;
      color: #555;
    }

    .times {
      .el-form-item__content {
        width: 250px;
      }
    }

    .el-form-item__content {
      min-width: 200px;
      // & > div {
      //   width: 100% !important;
      // }
    }

    .searchBtnBox {
      display: inline-block;
      margin: 20px 0 20px 20px;
    }
  }

  .pageContent {
    .tabs-list {
      padding-top: 20px;
    }

    .el-tabs__header {
      background: #fff;
    }

    .el-tabs__item {
      border: 1px solid #eee;
      margin: 0;
      border-left: 0;
      background: #fafbfc;
      color: #000;
    }

    .el-tabs__item:first-child {
      margin-left: 20px;
      border-left: 1px solid #eee;
    }

    .is-active {
      border-bottom: 0 !important;
      background: #fff;
      color: #307eff;
    }
  }

  .anjian label {
    padding: 0 !important;
    width: 100px !important;
  }

  .queryForm .el-checkbox {
    margin-right: 10px !important;
    width: auto !important;
  }

  .iszgfc .el-form-item__content {
    width: 120px;
  }
}
</style>
